package com.qf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/param")
@SessionAttributes("name")
public class ParamController {

    /*\
     * request 和 session 直接声明使用
     * */
    @RequestMapping("/test1")
    public String test1(HttpServletRequest request, HttpSession session) {
        request.setAttribute("name", "刘德华");
        session.setAttribute("age", 18);
        return "data";//转发到data.jsp
    }

    @RequestMapping("/test2")
    public String test2(Model model) {
        model.addAttribute("name", "周瑞发");// 和类上sessionAttributes 的多个值 有对应的 该对应的name值存储在了session里
        model.addAttribute("age", "19");
        return "data";//转发到data.jsp
    }

    /*
     * ModelAndView 能存储值 也存储了要跳转的页面
     * */
    @RequestMapping("/test3")
    public ModelAndView test3() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("name", "哈哈");
        modelAndView.addObject("age", 19);
        modelAndView.setViewName("forward:/data.jsp");
        return modelAndView;
    }

    @RequestMapping("/test4")
    public String test4(SessionStatus sessionStatus) {
        sessionStatus.setComplete();
        return "hello1";
    }

}
